package com.baiye.template.controller;

import com.baiye.template.common.R;
import com.baiye.template.pojo.OperationLog;
import com.baiye.template.pojo.Permission;
import com.baiye.template.pojo.Role;
import com.baiye.template.service.PermissionService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/7 16:43
 */
@RestController
@Api(value = "权限管理")
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @RequestMapping(value = "/getPermission",method = RequestMethod.POST)
    public R getPermission(@RequestBody Permission permission, @Param(value = "pageIndex")Integer pageIndex, @Param(value = "pageSize") Integer pageSize){
        List<Permission> i = permissionService.getPermission(permission,pageIndex,pageSize);
        PageInfo<Permission> pageInfo = new PageInfo<>(i);
        return new R().ok(pageInfo);
    }

    @RequestMapping(value = "/deletePermission",method = RequestMethod.GET)
    public R deletePermission(int id){
        permissionService.deleteById(id);
        return new R().ok();
    }
    @RequestMapping(value = "/addPermission",method = RequestMethod.POST)
    public R addPermission(@RequestBody Permission permission){
        permissionService.addPermission(permission);
        return new R().ok();
    }
    @RequestMapping(value = "/addPermissionToRole",method = RequestMethod.GET)
    public R addPermissionToRole(int roleId,int permissionId){
        permissionService.addPermissionToRole(roleId,permissionId);
        return new R().ok();
    }
    @RequestMapping(value = "/deletePermissionToRole",method = RequestMethod.GET)
    public R deletePermissionToRole(int roleId,int permissionId){
        permissionService.deletePermissionToRole(roleId,permissionId);
        return new R().ok();
    }

    @RequestMapping(value = "/getPermissionByRoleId",method = RequestMethod.GET)
    public R getPermissionByRoleId(Integer roleId){
        List<Permission> i = permissionService.getPermissionByRoleId(roleId);
        return new R().ok(i);
    }
    @RequestMapping(value = "/updatePermission",method = RequestMethod.POST)
    public R updatePermission(@RequestBody Permission permission){
        permissionService.updatePermission(permission);
        return new R().ok();
    }
}
